﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public partial class Users_UserManage : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = BasePage.WebTitle;
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            if (!IsPostBack)
            {
                Lbtn_Submit.Attributes.Add("onclick", "Mask()");
                Init_CustomerInfo(BasePage.GetValue(0));
            }
        }
        else
        {
            Response.Redirect("Login.aspx");
        }

    }
    #region 取FJSX设置
    private void Init_FJSX()
    {        
        dt.Columns.Add("ColomnDM", typeof(string));
        dt.Columns.Add("ColomnName", typeof(string));
        DataRow dr = dt.NewRow();
        dr["ColomnDM"] = "FJSX1";
        dr["ColomnName"] = "测试类型：";
        dt.Rows.Add(dr);
        DataRow dr2 = dt.NewRow();
        dr2["ColomnDM"] = "FJSX2";
        dr2["ColomnName"] = "测试类型2：";
        dt.Rows.Add(dr2);
    }
    #endregion
    #region 取省
    private void GetProvince(string select)
    {
        BLL.Province province = new BLL.Province();
        List<Model.Province> ps = new List<Model.Province>();
        ps = province.GetAllList();
        DropDownList1.DataSource = ps;
        DropDownList1.DataTextField = "ProName";
        DropDownList1.DataValueField = "ProID";
        DropDownList1.DataBind();
        if (select.Length > 0)
        {
            DropDownList1.SelectedValue = select;
        }
        else
        {
            DropDownList1.SelectedIndex = -1;
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex > -1)
        {
            GetCity(DropDownList1.SelectedValue,"");
            GetArea(DropDownList2.SelectedValue, "");
        }
    }
    #endregion
    #region 取市
    private void GetCity(string proid,string select)
    {
        if (proid.Length > 0)
        {
            BLL.City city = new BLL.City();
            List<Model.City> cs = new List<Model.City>();
            cs = city.GetAllList(proid);
            DropDownList2.Items.Clear();
            DropDownList2.Items.Add(new ListItem("请选择", "0"));
            DropDownList2.DataSource = cs;
            DropDownList2.DataTextField = "CityName";
            DropDownList2.DataValueField = "CityID";
            DropDownList2.DataBind();
            if (select.Length > 0)
            {
                DropDownList2.SelectedValue = select;
            }
            else
            {
                DropDownList2.SelectedIndex = -1;
            }
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList2.SelectedIndex > -1)
        {
            GetArea(DropDownList2.SelectedValue,"");
        }
    }
    #endregion
    #region 取地区

    private void GetArea(string cityid,string select)
    {
        if (cityid.Length > 0)
        {
            BLL.Area area = new BLL.Area();
            List<Model.Area> As = new List<Model.Area>();
            As = area.GetAllList(cityid);
            DropDownList3.Items.Clear();
            DropDownList3.Items.Add(new ListItem("请选择", "0"));
            DropDownList3.DataSource = As;
            DropDownList3.DataTextField = "AreaName";
            DropDownList3.DataValueField = "AreaID";
            DropDownList3.DataBind();
            if (select.Length > 0)
            {
                DropDownList3.SelectedValue = select;
            }
            else
            {
                DropDownList3.SelectedIndex = -1;
            }
        }
    }
    #endregion    
    
    #region 取学历

    private void GetEdu(string select)
    {
        BLL.V_EDUCATION edu = new BLL.V_EDUCATION();
        ddp_edu.DataSource = edu.GetAllList();
        ddp_edu.DataTextField = "MC";
        ddp_edu.DataValueField = "DM";
        ddp_edu.DataBind();
        if (select.Length > 0)
        {
            ddp_edu.SelectedValue = select;
        }
        else
        {
            ddp_edu.SelectedIndex = -1;
        }
    }
    #endregion
    #region 取职业

    private void GetOccupation(string select)
    {
        BLL.V_OCCUPATION opp = new BLL.V_OCCUPATION();
        ddp_job.DataSource = opp.GetAllList();
        ddp_job.DataTextField = "MC";
        ddp_job.DataValueField = "DM";
        ddp_job.DataBind();
        if (select.Length > 0)
        {
            ddp_job.SelectedValue = select;
        }
        else
        {
            ddp_job.SelectedIndex = -1;
        }
    }
    #endregion
    #region 初始化消费水平

    private void GetEarn(string select)
    {
        BLL.V_EARNING earn = new BLL.V_EARNING();
        ddp_buylevel.DataSource = earn.GetAllList();
        ddp_buylevel.DataTextField = "MC";
        ddp_buylevel.DataValueField = "DM";
        ddp_buylevel.DataBind();
        if (select.Length > 0)
        {
            ddp_buylevel.SelectedValue = select;
        }
        else
        {
            ddp_buylevel.SelectedIndex = -1;
        }
    }
    #endregion
    #region 初台化购物价位

    private void GetBuyPosition(string select)
    {
        BLL.V_WORTH wor = new BLL.V_WORTH();
        ddp_buyposition.DataSource = wor.GetAllList();
        ddp_buyposition.DataTextField = "MC";
        ddp_buyposition.DataValueField = "DM";
        ddp_buyposition.DataBind();
        if (select.Length > 0)
        {
            ddp_buyposition.SelectedValue = select;
        }
        else
        {
            ddp_buyposition.SelectedIndex = -1;
        }
    }
    #endregion
    #region 初始化服饰爱好

    private void GetLikeStyle(string select)
    {
        BLL.V_DUDS dud = new BLL.V_DUDS();
        ddp_likestyle.DataSource = dud.GetAllList();
        ddp_likestyle.DataTextField = "MC";
        ddp_likestyle.DataValueField = "DM";
        ddp_likestyle.DataBind();
        if (select.Length > 0)
        {
            ddp_likestyle.SelectedValue = select;
        }
        else
        {
            ddp_likestyle.SelectedIndex = -1;
        }
    }
    #endregion
    #region 初始化了解途径
    private void GetKnowFrom(string select)
    {
        BLL.V_KNOWS know = new BLL.V_KNOWS();
        rbt_knowfrom.DataSource = know.GetAllList();
        rbt_knowfrom.DataTextField = "MC";
        rbt_knowfrom.DataValueField = "DM";
        rbt_knowfrom.DataBind();
        if (select.Length > 0)
        {
            rbt_knowfrom.SelectedValue = select;
        }
        else
        {
            rbt_knowfrom.SelectedIndex = -1;
        }
    }
    #endregion
    #region 初始化会员信息

    private void Init_CustomerInfo(string UserCode)
    {
        BLL.V_CUSTOMER bll_customer = new BLL.V_CUSTOMER();
        Model.V_CUSTOMER model_customer = new Model.V_CUSTOMER();
        Model.V_CUSTOMER_AUTO model_auto = new Model.V_CUSTOMER_AUTO();
        model_auto = new BLL.V_CUSTOMER_AUTO().GetModel(UserCode);
        model_customer = bll_customer.GetModel(UserCode);
        Sex.SelectedValue = model_customer.SEX;
        txtRealName.Value = model_customer.GKMC;
        txtBirthday.Value = model_customer.SR.ToShortDateString();
        GetProvince(model_auto.VC_PROVINCE);
        GetCity(model_auto.VC_PROVINCE, model_auto.VC_CITY);
        GetArea(model_auto.VC_CITY, model_auto.VC_AREA);
        txtaddress.Text = model_auto.VC_ADDRESS;
        txtbz.Text = model_auto.VC_Note;
        txtPost.Value = model_customer.YB;
        txtTel.Value = model_customer.DH;
        txtMobile.Value = model_customer.SJ;
        txtEmail.Value = model_customer.EMAIL;
        ckbmerry.SelectedValue = model_customer.MARRY;
        GetEdu(model_customer.EDUCATION);
        GetOccupation(model_customer.OCCUPATION);
        GetEarn(model_customer.EARNING);
        GetBuyPosition(model_customer.WORTH);
        GetLikeStyle(model_customer.DUDS);
        GetKnowFrom(model_customer.KNOWS);
        //根据FJSX设置追加htmltable
        Init_FJSX();
        foreach (DataRow row in dt.Rows)
        {
            AddHtmlTable(row, model_customer);            
        }
    }
    #endregion
    #region 追加表格
    private void AddHtmlTable(DataRow row, Model.V_CUSTOMER customer)
    {
        string select = "";
        HtmlTableCell cell1 = new HtmlTableCell();
        cell1.Align = "right";
        cell1.InnerText = row["ColomnName"].ToString();
        HtmlTableCell cell2 = new HtmlTableCell();
        DropDownList ddp1 = new DropDownList();
        ddp1.ID = row["ColomnDM"].ToString();
        ddp1.Attributes.Add("onchange", "SetValue(this)");
        ddp1.Items.Add(new ListItem("请选择", "0"));
        ddp1.AppendDataBoundItems = true;
        switch (row["ColomnDM"].ToString())
        {
            case "FJSX1":
                BLL.V_FJSX1 fjsx1 = new BLL.V_FJSX1();
                ddp1.DataSource = fjsx1.GetAllList();
                select = customer.FJSX1;
                break;
            case "FJSX2":
                BLL.V_FJSX2 fjsx2 = new BLL.V_FJSX2();
                ddp1.DataSource = fjsx2.GetAllList();
                select = customer.FJSX2;
                break;
            case "FJSX3":
                ddp1.DataSource = new BLL.V_FJSX3().GetAllList();
                select = customer.FJSX3;
                break;
            case "FJSX4":
                ddp1.DataSource = new BLL.V_FJSX4().GetAllList();
                select = customer.FJSX4;
                break;
            case "FJSX5":
                ddp1.DataSource = new BLL.V_FJSX5().GetAllList();
                select = customer.FJSX5;
                break;
            case "FJSX6":
                ddp1.DataSource = new BLL.V_FJSX6().GetAllList();
                select = customer.FJSX6;
                break;
        }
        ddp1.DataTextField = "MC";
        ddp1.DataValueField = "DM";
        ddp1.DataBind();
        if (select.Length > 0)
        {
            ddp1.SelectedValue = select;
        }
        else
        {
            ddp1.SelectedIndex = -1;
        }
        cell2.Controls.Add(ddp1);
        HtmlTableCell cell3 = new HtmlTableCell();
        cell3.InnerText = "";        
        HtmlTableRow htmlrow =new HtmlTableRow();
        htmlrow.Cells.Add(cell1);
        htmlrow.Cells.Add(cell2);
        htmlrow.Cells.Add(cell3);
        InfoTable.Rows.Add(htmlrow);
    }
    #endregion   

    #region 更新会员资料
    private void Up_CustomerInfo()
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            WEB.ERP.V_CUSTOMER customer = new WEB.ERP.V_CUSTOMER();
            WEB.ERP.V_CUSTOMER_AUTO c_auto = new WEB.ERP.V_CUSTOMER_AUTO();
            Model.V_CUSTOMER_AUTO shop_c_auto = new BLL.V_CUSTOMER_AUTO().GetModel(BasePage.GetValue(0));
            Model.V_CUSTOMER shop_customer = new BLL.V_CUSTOMER().GetModel(BasePage.GetValue(0));
            BLL.Common.PropertyCopy(shop_c_auto, c_auto);
            BLL.Common.PropertyCopy(shop_customer, customer);
            customer.DM = BasePage.GetValue(0);
            customer.SEX = Sex.SelectedValue;
            customer.GKMC = BasePage.RemoveHtml(txtRealName.Value);
            customer.SR = txtBirthday.Value==""?DateTime.MinValue:DateTime.Parse(BasePage.RemoveHtml(txtBirthday.Value));
            string dz = "";
            if (DropDownList1.SelectedIndex > 0)
            {
                dz += DropDownList1.SelectedItem.Text;
            }
            if (DropDownList2.SelectedIndex > 0)
            {
                dz += DropDownList2.SelectedItem.Text;
            }
            if (DropDownList3.SelectedIndex > 0)
            {
                dz += DropDownList3.SelectedItem.Text;
            }
            customer.DZ = dz + BasePage.RemoveHtml(txtaddress.Text);
            customer.YB = BasePage.RemoveHtml(txtPost.Value);
            customer.DH = BasePage.RemoveHtml(txtTel.Value);
            customer.SJ = BasePage.RemoveHtml(txtMobile.Value);
            customer.EMAIL = BasePage.RemoveHtml(txtEmail.Value);
            customer.MARRY = ckbmerry.SelectedValue;
            if (ddp_edu.SelectedIndex > 0)
            {
                customer.EDUCATION = ddp_edu.SelectedValue;
            }
            if (ddp_job.SelectedIndex > 0)
            {
                customer.OCCUPATION = ddp_job.SelectedValue;
            }
            if (ddp_buylevel.SelectedIndex > 0)
            {
                customer.EARNING = ddp_buylevel.SelectedValue;
            }
            if (ddp_buyposition.SelectedIndex > 0)
            {
                customer.WORTH = ddp_buyposition.SelectedValue;
            }
            if (ddp_likestyle.SelectedIndex > 0)
            {
                customer.DUDS = ddp_likestyle.SelectedValue;
            }
            if (rbt_knowfrom.SelectedIndex > 0)
            {
                customer.KNOWS = rbt_knowfrom.SelectedValue;
            }
            string bb = Hidden1.Value;
            string[] temp = bb.Split('|');
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i].Length > 0)
                {
                    string[] re = temp[i].Split(',');
                    int index = re[0].LastIndexOf('_');
                    int len = re[0].Length;
                    string Attribute = re[0].Substring(index + 1, len - index - 1);
                    switch (Attribute)
                    {
                        case "FJSX1":
                            customer.FJSX1 = re[1];
                            break;
                        case "FJSX2":
                            customer.FJSX2 = re[1];
                            break;
                        case "FJSX3":
                            customer.FJSX3 = re[1];
                            break;
                        case "FJSX4":
                            customer.FJSX4 = re[1];
                            break;
                        case "FJSX5":
                            customer.FJSX5 = re[1];
                            break;
                        case "FJSX6":
                            customer.FJSX6 = re[1];
                            break;
                    }
                }
            }            
            c_auto.VC_CODE = BasePage.GetValue(0);
            c_auto.VC_PROVINCE = DropDownList1.SelectedValue;
            c_auto.VC_CITY = DropDownList2.SelectedValue;
            c_auto.VC_AREA = DropDownList3.SelectedValue;
            c_auto.VC_ADDRESS = BasePage.RemoveHtml(txtaddress.Text);
            c_auto.VC_Note = txtbz.Text;
            string code = ConfigurationManager.AppSettings["CODE"];
            WEB.ERP.Service erp = new WEB.ERP.Service();
            string result = erp.Customer_Update(code, customer, c_auto);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "Hid('" + result + "');", true);
        }
        else
        {
            Response.Redirect("Login.aspx");
        }

    }
    #endregion

    protected void Lbtn_Submit_Click(object sender, EventArgs e)
    {
        Up_CustomerInfo();
    }
}
